package org.richfaces.beans;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class PasswordValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		String password = (String) value;

		// Obtain the component and submitted value of the confirm password
		// component.
		UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm");

		String confirm = confirmComponent.getSubmittedValue().toString();

		// Check if they both are filled in.
		if (password == null || password.isEmpty() || confirm == null || confirm.isEmpty()) {
			return; // Let required="true" do its job.
		}

		if (!password.equals(confirm)) {
			confirmComponent.setValid(false);
			throw new ValidatorException(new FacesMessage( FacesMessage.SEVERITY_ERROR,"Password incorrecto","El password no es igual en los dos Campos."));
		}
	}

}
